package com.sjx.lib_network

import com.sjx.lib_network.bean.DailyWeatherResponse
import com.sjx.lib_network.bean.HourlyWeatherResponse
import com.sjx.lib_network.bean.WeatherResponse
import com.sjx.lib_network.http.HttpKey
import com.sjx.lib_network.http.HttpUrl
import com.sjx.lib_network.impl.HttpImplService
import com.sjx.lib_network.interceptor.HttpInterceptor
import okhttp3.OkHttpClient
import retrofit2.Callback
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

/**
 * FileName:HttpManager
 * Founder: sjx
 * Profile: 网络请求管理类
 */
object HttpManager {


    //创建客户端
    private fun getClient(): OkHttpClient {
        return OkHttpClient.Builder().addInterceptor(HttpInterceptor()).build()
    }

    private val retrofitCity by lazy {
        Retrofit.Builder()
            .client(getClient())
            .baseUrl(HttpUrl.WEATHER_BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
    }

    private val apiCity by lazy {
        retrofitCity.create(HttpImplService::class.java)
    }

    fun queryNow(location: String, callback: Callback<WeatherResponse>) {
        apiCity.queryNow(location, HttpKey.WEATHER_KEY).enqueue(callback)
    }

    fun queryHourly(location: String, callback: Callback<HourlyWeatherResponse>) {
        apiCity.queryHourly(location, HttpKey.WEATHER_KEY).enqueue(callback)
    }

    fun queryDaily(location: String, callback: Callback<DailyWeatherResponse>) {
        apiCity.queryDaily(location, HttpKey.WEATHER_KEY).enqueue(callback)
    }

}